﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using dbPoll_Application.ViewModels;
using dbPoll_Application.Models;
using System.Web.Routing;
using dbPoll_Application.Helpers;

namespace dbPoll_Application.Controllers
{
    public class ReportController : Controller
    {

        public ActionResult ResultsChart(int pollID, int questionID)
        {
            Question question = QuestionRepository.GetQuestionByID<Question>(questionID);
            IList<String> correctOptions = new List<String>();
            var poll = PollRepository.GetPollByID(pollID);
            if (poll.isTestPoll)
            {
                ViewData["pollID"] = poll.linkedPoll.pollID;
            }
            else
            {
                ViewData["pollID"] = pollID;
            }

            PollController p = new PollController();
            IDictionary<String, int> responses = p.QuestionResult(question);

            // Get the question chart for the question
            QuestionChart chart = question.resultsChart;

            // Get correct answers for the question
            correctOptions = question.getCorrect();
            
            if (chart.valueType.Equals("Count"))
            {
                return View("CountChart", new ReportViewModel(questionID, responses, correctOptions));
            }
            else
            {
                return View("PercentageChart", new ReportViewModel(questionID, responses, correctOptions));
            }
        }

        public ActionResult ResultsChartWeb(int pollID, int questionID)
        {
            Question question = QuestionRepository.GetQuestionByID<Question>(questionID);
            IList<String> correctOptions = question.getCorrect();
            var poll = PollRepository.GetPollByID(pollID);
            if (poll.isTestPoll)
            {
                ViewData["pollID"] = poll.linkedPoll.pollID;
            }
            else
            {
                ViewData["pollID"] = pollID;
            }
            ViewData["pollName"] = poll.name;
            ViewData["questionNumber"] = question.questionNumber;
            ViewData["questionCount"] = poll.questions.Where(m => m.deletionTime == null).Count<Question>();

            PollController p = new PollController();
            IDictionary<String, int> responses = p.QuestionResult(question);

            // Get the question chart for the question
            QuestionChart chart = question.resultsChart;

            if (chart.valueType.Equals("Count"))
            {
                return View("CountChartWeb", new ReportViewModel(questionID, responses, correctOptions));
            }
            else
            {
                return View("PercentageChartWeb", new ReportViewModel(questionID, responses, correctOptions));
            }
        }

        // If called on a non MCQ question, or an MCQ without a compare chart, 
        // returns the result chart for that question.
        public ActionResult ComparativeChart(int pollID, int questionID)
        {
            Question question = QuestionRepository.GetQuestionByID<Question>(questionID);
            IDictionary<String, int> demoResponses = new Dictionary<String, int>();
            IDictionary<String, int> compareResponses = new Dictionary<String, int>();
            var poll = PollRepository.GetPollByID(pollID);
            if (poll.isTestPoll)
            {
                ViewData["pollID"] = poll.linkedPoll.pollID;
            }
            else
            {
                ViewData["pollID"] = pollID;
            }
            
            // If it's an MCQ question (e.g., can be compared)
            if (question.type == QuestionType.Demographic || question.type == QuestionType.Custom ||
                question.type == QuestionType.Ranking)
            {
                // Get the two questions for comparison
                MultipleChoiceQuestion demoQuestion = QuestionRepository.GetQuestionByID<MultipleChoiceQuestion>(questionID);
                MultipleChoiceQuestion compareQuestion = demoQuestion.GetCompareWith();

                // Check that compare with is not null
                if (compareQuestion != null)
                {
                    // Get responses for the first question
                    foreach (var option in demoQuestion.options)
                    {
                        demoResponses.Add(option.answer, option.responses.Count);
                    }

                    // Get responses for the second question
                    foreach (var option in compareQuestion.options)
                    {
                        compareResponses.Add(option.answer, option.responses.Count);
                    }


                    return View("ComparativeChart", new ReportViewModel(questionID, demoResponses, compareResponses));
                }
            }
            
            return ResultsChart(pollID, questionID);

        }

        // If called on a non MCQ question, or an MCQ without a compare chart, 
        // returns the result chart for that question.
        public ActionResult ComparativeChartWeb(int pollID, int questionID)
        {
            Question question = QuestionRepository.GetQuestionByID<Question>(questionID);
            IDictionary<String, int> demoResponses = new Dictionary<String, int>();
            IDictionary<String, int> compareResponses = new Dictionary<String, int>();
            var poll = PollRepository.GetPollByID(pollID);
            if (poll.isTestPoll)
            {
                ViewData["pollID"] = poll.linkedPoll.pollID;
            }
            else
            {
                ViewData["pollID"] = pollID;
            }
            ViewData["pollName"] = poll.name;
            ViewData["questionNumber"] = question.questionNumber;
            ViewData["questionCount"] = poll.questions.Where(m => m.deletionTime == null).Count<Question>();

            // If it's an MCQ question (e.g., can be compared)
            if (question.type == QuestionType.Demographic || question.type == QuestionType.Custom ||
                question.type == QuestionType.Ranking)
            {
                // Get the two questions for comparison
                MultipleChoiceQuestion demoQuestion = QuestionRepository.GetQuestionByID<MultipleChoiceQuestion>(questionID);
                MultipleChoiceQuestion compareQuestion = demoQuestion.GetCompareWith();

                // Check that compare with is not null
                if (compareQuestion != null)
                {
                    // Get responses for the first question
                    foreach (var option in demoQuestion.options)
                    {
                        demoResponses.Add(option.answer, option.responses.Count);
                    }

                    // Get responses for the second question
                    foreach (var option in compareQuestion.options)
                    {
                        compareResponses.Add(option.answer, option.responses.Count);
                    }


                    return View("ComparativeChartWeb", new ReportViewModel(questionID, demoResponses, compareResponses));
                }
            }

            return ResultsChartWeb(pollID, questionID);

        }

    }
}
